using System;
using System.Drawing;
using System.Globalization;
using System.Xml;

namespace HWEngine.Configuration
{
	internal interface IStringFormatter
	{
		String ToString(object o);
		object FromString(String str, Type type);
	}

	public class DefaultStringFormatter : IStringFormatter
	{
		public string ToString(object o)
		{
			return Convert.ToString(o, CultureInfo.InvariantCulture);
		}

		public object FromString(string str, Type type)
		{
			return Convert.ChangeType(str, Type.GetTypeCode(type), CultureInfo.InvariantCulture);
		}
	}

	internal class ColorFormatter : IStringFormatter
	{
		public static String ColorToStr(Color color)
		{
			return color.ToArgb().ToString("X");
		}

		public static Color StrToColor(String str)
		{
			int i = Convert.ToInt32(str, 16);
			return Color.FromArgb(i);
		}

		public string ToString(object o)
		{
			return ColorToStr((Color) o);
		}

		public object FromString(string str, Type t)
		{
			return StrToColor(str);
		}
	}

	internal class EnumFormatter : IStringFormatter
	{
		public string ToString(object o)
		{
			return o.ToString();
		}

		public object FromString(string str, Type t)
		{
			return Enum.Parse(t, str);
		}

		public static T Parse<T>(string str)
		{
			return (T) Enum.Parse(typeof (T), str);
		}
	}

	internal class CustomXmlFormatter : IStringFormatter
	{
		public string ToString(object o)
		{
			return Serializer.Serialize(o);
		}

		public object FromString(string str, Type t)
		{
			return Serializer.Deserialize(str, t);
		}
	}
}